﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FloowCube : MonoBehaviour
{
    public Transform target;
    public float moveSpeed;
    private Vector3 pos;

    private Vector3 nowPos;
    private Vector3 startPos;
    private float time = 0;

    // Update is called once per frame
    void Update()
    {
        //第一种：先快后慢的形式
        //pos = this.transform.position;
        //pos.x = Mathf.Lerp(pos.x, target.position.x, Time.deltaTime * moveSpeed);
        //pos.y = Mathf.Lerp(pos.y, target.position.y, Time.deltaTime * moveSpeed);
        //pos.z = Mathf.Lerp(pos.z, target.position.z, Time.deltaTime * moveSpeed);
        //this.transform.position = pos;

        //第二种：匀速变化
        if (nowPos != target.transform.position)
        {
            time = 0;
            nowPos = target.transform.position;
            startPos = this.transform.position;
        }
        pos = this.transform.position;
        time += Time.deltaTime;
        pos.x = Mathf.Lerp(startPos.x, nowPos.x, time);
        pos.y = Mathf.Lerp(startPos.y, nowPos.y, time);
        pos.z = Mathf.Lerp(startPos.z, nowPos.z, time);
        this.transform.position = pos;
    }
}
